package ContainerLayer;
import java.util.ArrayList;

/**
 * Write a description of class CostumerContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CostumerContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Costumer> costumers;
    private static CostumerContainer instance;

    /**
     * Constructor for objects of class CostumerContainer
     */
    public CostumerContainer()
    {
        // initialise instance variables
        costumers = new ArrayList<Costumer>();
    }

    //     private CostumerContainer()
    //     {
    //         costumers = new ArrayList<Costumer>();
    //     }

    public static CostumerContainer getInstance()
    {
        if(instance == null)
        {
            instance = new CostumerContainer();
        }
        return instance;   
    }

    public void addCostumer(Costumer co)
    {

        if(!costumers.equals(co))
        {
            costumers.add(co);
        }
        else 
        {
            System.out.println("There is already a costumer with the typed name");
        } 

    }

    public Costumer readCostumer(long id)
    {
        int index = 0; 
        int costumerIndex = 0;
        boolean found = false;

        while(index < costumers.size() && !found)
        {
            if(costumers.get(index).getID() == id)
            {                
                found = true; 
                costumerIndex = index;
            }
            index++;
        }
        if(found)
        {
            return costumers.get(costumerIndex);
        }

        else
        {
            return null;
        }
    }

    public void updateCostumer(long id, String name, String address, String mail, int phoneNo)
    {
        int index = 0;
        int costumerIndex = 0;
        boolean found = false;

        while(index < costumers.size() && !found)
        {
            if(costumers.get(index).getID() == id)
            {
                found = true;
                costumerIndex = index;
            }          
            index++;
        }

        //If found, the filled parametre will be set
        // if not, the unfilled parametre will not be set
        if(found)
        {
            Costumer c = costumers.get(costumerIndex);
            if(name != null)
            {
                c.setName(name);
            }
            if(address != null)
            {
                c.setAddress(address);
            }
            if(mail != null)
            {
                c.setMail(mail);
            }
            if(phoneNo != 0)
            {
                c.setPhoneNo(phoneNo);
            }

        }

        else
        {
            System.out.println("There is no costumer with the typed ID,  - try again with another ID");
        }
    }

    public void deleteCotumer(long id)
    {   
        int index = 0;
        boolean deleted = false;
        while(index < costumers.size() && !deleted)
        {
            if(costumers.get(index).getID() == id)
            {
                System.out.println("     >>The following costumer with the typed ID is deleted!<<");
                costumers.remove(index);
                deleted = true;
            }
            index++;                       
        }       

        if(!deleted)
        {
            System.out.println("There is no costumer with the typed ID, - try again with another ID");
        }

    }
    
    public void printOutCostumerID(long id)
    {
        int index = 0; 
        int costumerIndex = 0;
        boolean found = false;

        while(index < costumers.size() && !found)
        {
            if(costumers.get(index).getID() == id)
            {                
                found = true; 
                costumerIndex = index;
            }
            index++;
        }
        if(found)
        {
            Costumer c = costumers.get(costumerIndex);
            System.out.println("############################################################");
                System.out.println("#     Costumer ID: "+c.getID());
                System.out.println("#  ---------------------------------------------------------");
                System.out.println("#        name: "+c.getName());
                System.out.println("#     address: "+c.getAddress());
                System.out.println("#    phone no: "+c.getPhoneNo());
                System.out.println("#        mail: "+c.getMail());                
                System.out.println("#");                
                System.out.println("#################   THE END  for costumer: "+c.getID() );
                System.out.println("");  
       }    
    }

    public void printOutAllCostumer()
    {
        
        System.out.println("    SHOWS INFORMATION ABOUT ALL REGISTERED COSTUMER");
        if(costumers.size() != 0)
        {
            for(Costumer c : costumers)
            {
                System.out.println("############################################################");
                System.out.println("#     Costumer ID: "+c.getID());
                System.out.println("#  ---------------------------------------------------------");
                System.out.println("#        name: "+c.getName());
                System.out.println("#     address: "+c.getAddress());
                System.out.println("#    phone no: "+c.getPhoneNo());
                System.out.println("#        mail: "+c.getMail());                
                System.out.println("#");                
                System.out.println("#################   THE END  for costumer: "+c.getID() );
                System.out.println("");                
            }
            
            
        }
        else
        {
            System.out.println("           >>There is no costumers registred<<");
            System.out.println("");
        }
    }

    public int getSizeCostumer()
    {
        if(costumers != null)
        {
            return costumers.size();
        }
        else
        {
            return 0;
        }
        
    }

    
}
